﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleConfigurator.Database;
using VehicleConfigurator.Model;

namespace VehicleConfigurator
{
    public partial class FrmLang : Form
    {
        public FrmLang()
        {
            InitializeComponent();
        }

        private void FrmLang_Load(object sender, EventArgs e)
        {
            this.LoadData();
            ReFormatGrid();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {


        }
        void ReFormatGrid()
        {
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.ScrollBars = ScrollBars.Both;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
            {

                dataGridView1.Rows.RemoveAt(item.Index);


            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //DialogResult result1 = MessageBox.Show("Are you sure to save the change?", "Important Question", MessageBoxButtons.YesNo);
            //if (result1 == DialogResult.Yes)
            //{
            //    this.UpdateData();
            //}
            this.UpdateData();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)dataGridView1.DataSource;
            Language l = new Language(-1);
            dt.Rows.Add(l.OID, l.Code, l.Name, l.CultureInfo, l.IsDefault);
            dataGridView1.DataSource = dt;

        }

       

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void UpdateData()
        {
            clsDb db = new clsDb();

            db.updateData("language", this.dataGridView1);
            this.LoadData();
        }
        public void LoadData()
        {
            string sql = " SELECT oid,code,name,CultureInfo, CASE IsDefault WHEN 1 THEN  TRUE	WHEN 0 THEN FALSE END AS IsDefault FROM language";
            clsDb db = new clsDb();
            db.LoadData(sql, this.dataGridView1);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
            if (dataGridView1.Columns[e.ColumnIndex].Name == "isDefault"
                && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0")
            {
                //MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
                foreach (DataGridViewRow r in dataGridView1.Rows)
                {
                    if (r.Cells["isDefault"].Value.ToString().Equals("1") && r.Index != e.RowIndex)
                    {
                        r.Cells["isDefault"].Value = false;
                    }
                }
            }
        }


    }
}
